
import { useBoolean } from "ahooks";
import { View } from "@tarojs/components";
import Taro, { useRouter } from "@tarojs/taro"

import Page from "@/components/common/Page";
import ByButton from "@/components/ui/ByButton";
import ListView from "@/components/app/ListView";
import TodoCard from "@/components/app/TodoCard";
import PageHeader from "@/components/common/PageHeader";
import PopupPlanExecution from "./components/PopupPlanExecution";

import ApiWorkflow from "@/api/workflow";
import useFetch from "@/hooks/useFetch";

import { TaskItem } from "@/types/global";

export default () => {

  const { params } = useRouter();
  const remindId = params.remindId ? Number(params.remindId) : undefined

  const { loading, data, error } = useFetch<{ task: TaskItem }>(ApiWorkflow.taskRemind, {
    manual: !remindId, defaultParams: [{ remindId }]
  })

  const [visible, { setTrue, setFalse }] = useBoolean()
  const task = data?.task

  return (
    <Page slotHeader={<PageHeader />}>

      <View className="p-4">
        <ListView loading={loading} isEmpty={!task}
          emptyText={error?.msg}
          slotEmpty={
            <ByButton size="large" className="w-2/3 mt-6" to="/pages/index/index" toFn={Taro.switchTab}>回到首页</ByButton>
          }>
          {
            task && <TodoCard
              source={task}
              onClick={setTrue}
            />
          }
        </ListView>
      </View>

      {task?.itemType === 2 && visible && (
        <PopupPlanExecution
          intentCustomerId={task.intentCustomerId}
          onCancel={setFalse}
        />
      )}
    </Page>
  )
}
